<?php

	// +----------------------------------------------------------------------
	// |  易支付
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

namespace yicmf\tools;

	use think\facade\Config;

	class Pay
	{

		protected $app_id;
		protected $app_key;

		public function __construct($app_id, $app_key)
		{
			$this->app_id = $app_id;
			$this->app_key = $app_key;
		}

		/**
		 * 创建支付
		 * @param array $param
		 * @return array
		 */
		public function create($param)
		{
			$param['app_id'] = $this->app_id;
			$param['sign'] = $this->_sign($param);
			$result = HttpClient::post('http://pay.yicmf.com/api/create', $param);
			if ($result['code'] == 200) {
				return $result['content'];
			} else {
				return false;
			}
		}

		/**
		 * 创建支付
		 * @param array $param
		 * @return bool
		 */
		public function check($param)
		{
			$sign = $this->_sign($param);
			return $sign === $param['sign'];
		}

		protected function _sign($params)
		{
			$signOriginStr = '';
			ksort($params);
			foreach ($params as $key => $value) {
				if (empty($key) || $key == 'sign') {
					continue;
				}
				$signOriginStr = $signOriginStr . $key . '=' . $value . '&';
			}
			return strtolower(md5($signOriginStr . 'key=' . $this->app_key));
		}
	}
